package studio.raptor.dispatcher.server.handler.consumption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import studio.raptor.databus.command.EventMetaData;
import studio.raptor.dispatcher.common.constant.EventStatus;
import studio.raptor.dispatcher.server.context.HandlerContext;
import studio.raptor.dispatcher.server.handler.Handler;
import studio.raptor.dispatcher.server.service.GraphService;

/**
 * 对事件进行排序
 *
 * @author jack
 * @since 1.0
 */
@Component
public class SortHandler extends Handler {

  private static final Logger log = LoggerFactory.getLogger(SortHandler.class);

  @Autowired
  GraphService sortService;

  @Override
  public boolean process(HandlerContext context) {
    boolean result = false;
    String code = context.getCode();
    EventMetaData metaData = context.getEvent().getEventMetaData();
    String orderId = metaData.getBusinessNo();
    try {
      sortService.sortEvent(code, metaData, EventStatus.WAITED);
      result = true;
    } catch (Exception e) {
      log.error("sort event:[{}] exception", orderId, e);
    }
    return result;
  }
}
